package vip.fuxk.blog.config;

import org.noear.snack.core.Feature;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.serialization.snack3.SnackActionExecutor;
import org.noear.solon.serialization.snack3.SnackRenderFactory;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Configuration
public class JsonMsgConfig {
//    @Bean
    public void jsonInit(@Inject SnackRenderFactory factory, @Inject SnackActionExecutor executor){
        //示例1：通过转换器，做简单类型的定制
        factory.addConvertor(Date.class, s -> s.getTime());

        factory.addConvertor(LocalDate.class, s -> s.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        factory.addConvertor(Double.class, s -> String.valueOf(s));

        //示例2：通过编码器，做复杂类型的原生定制（基于框架原生接口）
        factory.addEncoder(Date.class, (data, node) -> node.val().setNumber(data.getTime()));

        //示例3：调整特性（例，添加枚举序列化为名字的特性）
        factory.addFeatures(Feature.EnumUsingName);  //增加特性
        //factory.removeFeatures(Feature.BrowserCompatible); //移除特性
        //factory.setFeatures(...); //重设特性
        
        //factory.config()...
        //executor.config()...
    }
}